package org.dfl.taverna;

import java.util.Vector;
import javax.swing.tree.DefaultMutableTreeNode;
import org.embl.ebi.escience.scuflworkers.ProcessorFactory;

/**
 *
 * @author Lukas
 */
public class ProcessorTreeNode {

    private TavernaProcessor processor;
    private String label;
    private Vector<ProcessorTreeNode> children;

    private static String stripTags(String string) {
        return string.replaceAll("<[^>]*>", "");
    }

    public ProcessorTreeNode(String label) {
        processor = null;
        this.label = label;
        children = new Vector<ProcessorTreeNode>();
    }
    
    public ProcessorTreeNode(ProcessorFactory factory) {
        processor = new TavernaProcessor(factory);
        label = processor.getLabel();
        children = new Vector<ProcessorTreeNode>();
    }

    public ProcessorTreeNode(DefaultMutableTreeNode scavenger) {
        processor = null;
        label = "(empty)";
        children = new Vector<ProcessorTreeNode>();
        ProcessorFactory factory = null;
        
        if (scavenger.getChildCount() == 0 && scavenger.getUserObject() instanceof ProcessorFactory) {
            factory = (ProcessorFactory) scavenger.getUserObject();
            processor = new TavernaProcessor(factory);
            label = processor.getLabel();
        } else if (scavenger.getChildCount() > 0) {
            label = stripTags(scavenger.toString());

            for (int i = 0; i < scavenger.getChildCount(); ++i) {
                children.add(new ProcessorTreeNode((DefaultMutableTreeNode) scavenger.getChildAt(i)));
            }
        }
    }

    public String getLabel() {
        return label;
    }
    
    public TavernaProcessor getProcessor() {
        return processor;
    }

    public int getChildrenCount() {
        return children.size();
    }

    public ProcessorTreeNode getChildNode(int index) {
        return children.elementAt(index);
    }

    public void addChildNode(ProcessorTreeNode node) {
        children.add(node);
    }
}
